using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.IO;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Presentation;
using IGlobalist.Cms.Common;

namespace IGlobalist.Cms
{
    public partial class _Default : BaseCmsPage
    {
        protected override void OnLoad(EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("rememberUser");
            HttpCookie userLgin = HttpContext.Current.Request.Cookies.Get("memberLogin");
            
            string[] uscurrent ={"",""};
                                    
            if (cookie != null)
            {
                string value = cookie.Value;
                uscurrent = value.Split(',');
                if (userLgin != null)
                    if(userLgin.Value=="1")
                        IGlobalistMembership.ValidateUserSite(Convert.ToString(uscurrent[0]), Convert.ToString(uscurrent[1]));
            }
            ExtProperties.Add("RememberUser", uscurrent[0]);
            ExtProperties.Add("RememberPass", uscurrent[1]);

          
            //if (String.IsNullOrEmpty(Request.QueryString["pid"]))
            //{
            //    Response.Redirect("~/0_0_0/Default.aspx", true);
            //}
            ExtProperties.Add("SiteFooter", SiteConfig.SiteFooter);
            ExtProperties.Add("SiteTitle", SiteConfig.SiteTitle);
            ExtProperties.Add("Keywords", SiteConfig.MetaKeyWords);
            ExtProperties.Add("Description", SiteConfig.MetaDescription);
            ExtProperties.Add("Payment", SiteConfig.Payment);

            IList countries = new List<ListItem>();
            XmlDocument xDoc = new XmlDocument();
            string path = HttpContext.Current.Request.PhysicalApplicationPath;
            xDoc.Load(path + "Admin/Xml/Country.xml");

            foreach (XmlNode xNode in xDoc.DocumentElement.ChildNodes)
            {
                XmlNode xItemNode = xNode;
                countries.Add(new ListItem( xItemNode.Attributes["name"].Value ,xItemNode.Attributes["value"].Value));
            }

            ExtProperties.Add("Countries", countries);

            

           
            if (Session[SessionVariable.ShoppingCart.CurrentShoppingCart] != null)
            {
                IOrder order = (IOrder)Session[SessionVariable.ShoppingCart.CurrentShoppingCart];
                ExtProperties.Add("Order", order);
            }
            if (Convert.ToString(Request["shopping"])=="1") 
                ExtProperties.Add("Shopping",1);
            if (Convert.ToString(Request["checkout"]) == "1")
                ExtProperties.Add("Checkout", 1);

            if (Convert.ToString(Request["sumOrder"]) != null)
                ExtProperties.Add("SumOrder", Request["sumOrder"]);

            if (Session["VideoList"]!=null)
            {
                ExtProperties.Add("Search", 1);
                if (Session["sVideo"]!=null)
                    ExtProperties.Add("tbSearch", Session["sVideo"]);
                VideoList VideoSearch=new VideoList();
                VideoSearch = (VideoList)Session["VideoList"];
                ExtProperties.Add("VideoSearch",(VideoList) Session["VideoList"]);
                Session["VideoList"] = null;
                Session["sVideo"] = null;
            }
            else
                ExtProperties.Add("tbSearch", "");

            if (Convert.ToString(Request["login"]) !=null)
                ExtProperties.Add("Login", Request["login"]);

            if (Convert.ToString(Request["signup"]) !=null)
                ExtProperties.Add("Signup", Request["signup"]);

            if (Convert.ToString(Request["success"])!=null)
                ExtProperties.Add("Success", Request["success"]);

            if (Convert.ToString(Request["gallery"]) != null)
                ExtProperties.Add("Gallery", Request["gallery"]);

            if (Convert.ToString(Request["account"]) != null)
                ExtProperties.Add("Account", Request["account"]);

            if (Convert.ToString(Request["edit"]) != null)
                ExtProperties.Add("Edit", Request["edit"]);

            if (Convert.ToString(Request["quote"]) != null)
                ExtProperties.Add("Quote", Request["quote"]);

            if (Convert.ToString(Request["video"]) != null)
                ExtProperties.Add("MyVideo", Request["video"]);
            
            if (Convert.ToString(Request["thread"]) == "1")
                ExtProperties.Add("thread", "1");

            if (Convert.ToString(Request["editThread"]) == "1")
                ExtProperties.Add("editThread", "1");
            
            if (Convert.ToString(Request["topic"]) == "1")
                ExtProperties.Add("topic", "1");

            if (Convert.ToString(Request["searchForum"]) == "1")
                ExtProperties.Add("searchForum", "1");
            
            if (Convert.ToString(Request["content"]) == "1")
                ExtProperties.Add("content", "1");

                       
            if (Convert.ToString(Request["forum"]) == "1")
                ExtProperties.Add("forum", "1");

            if (Request["editF"]!=null)
                ExtProperties.Add("EditF", Request["editF"]);


            if (Convert.ToString(Request["MemberRegister"]) == "1")
                ExtProperties.Add("MemberRegister", "1");

            if (Convert.ToString(Request["getpass"]) != null)
                ExtProperties.Add("getpass", Request["getpass"]);

            if (Convert.ToString(Request["MyProfile"]) == "1")
                ExtProperties.Add("MyProfile", "1");
            

            if (Convert.ToString(Request["userLogin"]) != null)
                ExtProperties.Add("userLogin", Request["userLogin"]);

            if (Convert.ToString(Request["logout"]) == "1")
            {
                string url = Request.UrlReferrer.PathAndQuery;
                int totalString = url.LastIndexOf('?');
                if (totalString>1)
                url = url.Substring(0, totalString);
                IGlobalistMembership.Logout();
                HttpCookie userLogin = new HttpCookie("memberLogin", "2");
                HttpContext.Current.Response.Cookies.Set(userLogin);
                url = string.Format("{0}", url);
                Response.Redirect(url);

            }

            if (Session["content"] != null)
            {
                ExtProperties.Add("Content", (string)Session["content"]);
                Session["content"] = null;
            }
            
            #region list file of forum
            try
            {
                if (IGlobalistMembership.GetUser().ID > 0)
                {
                    DirectoryInfo m_dir = new System.IO.DirectoryInfo(Server.MapPath("../CmsData/Forum/" + IGlobalistMembership.GetUser().ID));
                    ArrayList listFile = new ArrayList();
                    foreach (FileInfo f in m_dir.GetFiles())
                    {

                        listFile.Add(new ListItem(f.Name, f.Name));
                    }
                    ExtProperties.Add("ListFile", listFile);
                }
            }
            catch
            {
            }
            #endregion

            IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
            IList<IUser> listUser = UserController.GetUserList(Convert.ToInt64(list[0].ID));
            ExtProperties.Add("TotalUser", listUser.Count);
            ExtProperties.Add("UserNew", listUser[listUser.Count-1]);


            try
            {
                IUser user = IGlobalistMembership.GetUser();
                if (user!=null)
                {
                    int isAdmin=0;
                    string[] sRole = user.Roles;

                    if (sRole.Length > 0)
                    {
                        for (int i = 0; i < sRole.Length; i++)
                        {
                            IRole r = RoleController.GetRole(Convert.ToInt64(sRole[i]));
                            if (r.Name == "SysAdmin")
                            {
                                isAdmin = 1;
                                break;
                            }
                        }
                    }
                    else
                        isAdmin = 0;
                    
                    ExtProperties.Add("isAdmin", isAdmin);
                    ExtProperties.Add("User", user);
                    Session[SessionVariable.User.CurrentUserLoggedOn] = user;
                }
                base.OnLoad(e);
            }
            catch
            {
            }


           
        }
        
    }
}
